package com.morgan.recyclerviewdemo.itemdecoration

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.morgan.recyclerviewdemo.utils.DeviceUtil


/**
 *description：
 *<br>author：caowugao
 *<br>time：2020/8/9 0009 14:26
 */
class SimpleGrideItemDecoration(private val mContext : Context) : RecyclerView.ItemDecoration() {

    private val mPaint: Paint = Paint(Paint.ANTI_ALIAS_FLAG)
    //第一行距离RecyclerView顶部的距离
    var mFirstRowMargin = DeviceUtil.dp2px(mContext, 5f)
    //最后一行的距离
    var mTheLastRowMargin = DeviceUtil.dp2px(mContext, 5f)
    //第一列距离RecyclerView左边的距离
//    var mFirstColumnMargin = DeviceUtil.dp2px(mContext, 5f)
    var mFirstColumnMargin = DeviceUtil.dp2px(mContext, 20f)
    //最后一列距离RecyclerView右边的距离
//    var mTheLastColumnMargin = DeviceUtil.dp2px(mContext, 5f)
    var mTheLastColumnMargin = DeviceUtil.dp2px(mContext, 20f)
    //水平方向两个item之间的距离
//    var mHorizontalMargin = DeviceUtil.dp2px(mContext, 5f)
    var mHorizontalMargin = DeviceUtil.dp2px(mContext, 15f)
    //竖直方向两个item之间的距离
//    var mVerticalMargin = DeviceUtil.dp2px(mContext, 5f)
    var mVerticalMargin = DeviceUtil.dp2px(mContext, 15f)

    var mFirstRowMarginColor  = Color.RED
    var mTheLastRowMarginColor  = Color.GREEN

    var mFirstColumnMarginColor  = Color.BLACK
    var mTheLastColumnMarginColor  = Color.GRAY

    var mHorizontalMarginColor  = Color.BLUE
    var mVerticalMarginColor  = Color.YELLOW

    private val mRect : Rect = Rect()

    class Builder() {

    }

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        if (parent.adapter == null
            || parent.adapter!!.itemCount == 0
            || parent.layoutManager == null
            || (parent.layoutManager !is GridLayoutManager && parent.layoutManager !is StaggeredGridLayoutManager)) {
            return super.getItemOffsets(outRect, view, parent, state)
        }


        val childParams = view.layoutParams
        if (childParams == null || childParams !is RecyclerView.LayoutParams) {
            return
        }
        val span = getSpanCount(parent)
        if (span <= 0) {
            return
        }
        val itemCount = parent.adapter!!.itemCount
        val childPos = childParams.viewLayoutPosition

        var left = 0
        var top = 0
        var right = 0
        var bottom = 0
        left = if (isFirstColumn(childPos, span)) mFirstColumnMargin else mHorizontalMargin
        top = if (isFirstRow(childPos, span)) mFirstRowMargin else mVerticalMargin

//        right = if (isTheLastColumn(childPos, span)) mTheLastColumnMargin else mHorizontalMargin
//        bottom = if (isTheLastRow(childPos, span, itemCount)) mTheLastRowMargin else mVerticalMargin

        right = if (isTheLastColumn(childPos, span)) mTheLastColumnMargin else 0
        bottom = if (isTheLastRow(childPos, span, itemCount)) mTheLastRowMargin else 0


        outRect.set(left, top, right, bottom)

    }

    /**
     * 获取列数
     * @param parent
     * @return
     */
    private fun getSpanCount(parent: RecyclerView): Int {
        var spanCount = -1
        val layoutManager = parent.layoutManager
        if (layoutManager is GridLayoutManager) {
            spanCount = layoutManager.spanCount
        } else if (layoutManager is StaggeredGridLayoutManager) {
            spanCount = layoutManager.spanCount
        }
        return spanCount
    }

    private  inline fun isFirstRow(pos : Int, span : Int) : Boolean = pos / span == 0

    private inline fun isTheLastRow(pos : Int, span : Int, itemCount : Int) : Boolean = itemCount - 1 - pos < span

    private inline fun isFirstColumn(pos : Int, span : Int) : Boolean = pos % span == 0

    private inline fun isTheLastColumn(pos : Int, span : Int) : Boolean = (pos + 1) % span ==  0


    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        if (parent.adapter == null
            || parent.adapter!!.itemCount == 0
            || parent.layoutManager == null
            || (parent.layoutManager !is GridLayoutManager && parent.layoutManager !is StaggeredGridLayoutManager)) {
            return super.onDraw(c, parent, state)
        }
        val span = getSpanCount(parent)
        if (span <= 0) {
            return
        }
        val childCount = parent.adapter!!.itemCount
        val rang = 0..childCount - 1
        for (i in rang) {
            val child = parent.getChildAt(i)
            if (child == null) {
                continue
            }
            val childParams = child.layoutParams
            if (childParams == null || childParams !is RecyclerView.LayoutParams) {
                continue
            }
            val childPos = childParams.viewLayoutPosition
            drawChild(c, parent, childCount, child, childPos, childParams, span)
        }

    }

    private fun drawChild(
        c: Canvas,
        parent: RecyclerView,
        childCount: Int,
        child: View,
        childPos: Int,
        childParams: RecyclerView.LayoutParams,
        span: Int
    ) {
        //画第一行系列的view的背景
        if (isFirstRow(childPos, span)) {
            drawFirstRow(c, parent, childCount, span, child, childPos, childParams)
            return
        }

        //画最后一行系列view的背景
        if (isTheLastRow(childPos, span, childCount)) {
            drawTheLastRow(c, parent, span, child, childPos, childParams)
            return
        }

        //画 非第一行 及 非最后一行 系列view的背景
       drawOtherRow(c, parent, childCount, span,  child, childPos, childParams)

    }

    private fun drawOtherRow(
        c: Canvas,
        parent: RecyclerView,
        childCount: Int,
        span: Int,
        child: View,
        childPos: Int,
        childParams: RecyclerView.LayoutParams
    ) {
        val parentParams = parent.layoutParams
        val parentPaddingLeft = parent.paddingLeft
        val parentPaddingTop = parent.paddingTop
        val parentPaddingRight = parent.paddingRight
        val parentPaddingBottom = parent.paddingBottom

        val childTranslationX = child.translationX
        val childTranslationY = child.translationY

        var left = 0
        var top = 0
        var right = 0
        var bottom = 0

        //画view顶部背景
        if (isFirstColumn(childPos, span)) {
            left = parentPaddingLeft + childTranslationX.toInt()
        } else {
            left = child.left - childParams.leftMargin - mHorizontalMargin + childTranslationX.toInt()
        }
        top = child.top - childParams.topMargin - mVerticalMargin + childTranslationY.toInt()
//        right = child.right + childParams.rightMargin + (if (isTheLastColumn(childPos, span)) mTheLastColumnMargin else mHorizontalMargin) + childTranslationX.toInt()
        right = child.right + childParams.rightMargin + (if (isTheLastColumn(childPos, span)) mTheLastColumnMargin else 0) + childTranslationX.toInt()
        bottom = child.top - childParams.topMargin + childTranslationY.toInt()
        mRect.set(left, top, right, bottom)
        mPaint.color = mVerticalMarginColor
        if (mRect.height() > 0) {
            c.drawRect(mRect, mPaint)
        }

        //画左边view的背景
        right = child.left - childParams.leftMargin + childTranslationX.toInt()
        top = child.top + childTranslationY.toInt()
        bottom = child.bottom + childTranslationY.toInt()
        mRect.set(left, top, right, bottom)
        mPaint.color = if (isFirstColumn(childPos, span)) mFirstColumnMarginColor else mHorizontalMarginColor
        if (mRect.width() > 0) {
            c.drawRect(mRect, mPaint)
        }

        //如果是最后一列，画view右边的背景
        if (isTheLastColumn(childPos, span)) {
            left = child.right + childParams.rightMargin + childTranslationX.toInt()
            top = child.top + childTranslationY.toInt()
//        right = parent.measuredWidth - parentPaddingRight + childTranslationX.toInt()
            right = child.right + childParams.rightMargin + mTheLastColumnMargin + childTranslationX.toInt()
            bottom = child.bottom + childTranslationY.toInt()
            mRect.set(left, top, right, bottom)
            mPaint.color = mTheLastColumnMarginColor
            if (mRect.width() > 0) {
                c.drawRect(mRect, mPaint)
            }
        }
    }

    private fun drawTheLastRow(
        c: Canvas,
        parent: RecyclerView,
        span: Int,
        child: View,
        childPos: Int,
        childParams: RecyclerView.LayoutParams
    ) {
        val parentParams = parent.layoutParams
        val parentPaddingLeft = parent.paddingLeft
        val parentPaddingTop = parent.paddingTop
        val parentPaddingRight = parent.paddingRight
        val parentPaddingBottom = parent.paddingBottom


        val childTranslationX = child.translationX
        val childTranslationY = child.translationY

        var left = 0
        var top = 0
        var right = 0
        var bottom = 0
        //画view顶部的背景
        if (isFirstColumn(childPos, span)) {
            left = parentPaddingLeft + childTranslationX.toInt()
        } else {
            left = child.left - childParams.leftMargin - mHorizontalMargin + childTranslationX.toInt()
        }
        top = child.top - childParams.topMargin - mVerticalMargin + childTranslationY.toInt()
//        right = child.right + childParams.rightMargin + (if (isTheLastColumn(childPos, span)) mTheLastColumnMargin else mHorizontalMargin) + childTranslationX.toInt()
        right = child.right + childParams.rightMargin + (if (isTheLastColumn(childPos, span)) mTheLastColumnMargin else 0) + childTranslationX.toInt()
        bottom = child.top - childParams.topMargin + childTranslationY.toInt()
        mRect.set(left, top, right, bottom)
        mPaint.color = mVerticalMarginColor
        if (mRect.height() > 0) {
            c.drawRect(mRect, mPaint)
        }

        //画view左边的背景
        right = child.left - childParams.leftMargin + childTranslationX.toInt()
        top = child.top + childTranslationY.toInt()
        bottom = child.bottom + childTranslationY.toInt()
        mRect.set(left, top, right, bottom)
        mPaint.color = if (isFirstColumn(childPos, span)) mFirstColumnMarginColor else mHorizontalMarginColor
        if (mRect.width() > 0) {
            c.drawRect(mRect, mPaint)
        }

        //如果是最后一行，并且是最后一列， 画view右边背景
        if (isTheLastColumn(childPos, span)) {
            left = child.right + childParams.rightMargin + childTranslationX.toInt()
            top = child.top + childTranslationY.toInt()
//            right = parent.measuredWidth - parentPaddingRight + childTranslationX.toInt()
            right = child.right + childParams.rightMargin + mTheLastColumnMargin + childTranslationX.toInt()
            bottom = child.bottom + childTranslationY.toInt()
            mRect.set(left, top, right, bottom)
            mPaint.color = mTheLastColumnMarginColor
            if (mRect.width() > 0) {
                c.drawRect(mRect, mPaint)
            }
        }

        //画view底部背景
        if (isFirstColumn(childPos, span)) {
            left = parentPaddingLeft + childTranslationX.toInt()
        } else {
            left = child.left - childParams.leftMargin - mHorizontalMargin + childTranslationX.toInt()
        }
        top = child.bottom + childParams.bottomMargin + childTranslationY.toInt()
//        right = child.right + childParams.rightMargin + (if (isTheLastColumn(childPos, span)) mTheLastColumnMargin else mHorizontalMargin) + childTranslationX.toInt()
        right = child.right + childParams.rightMargin + (if (isTheLastColumn(childPos, span)) mTheLastColumnMargin else 0) + childTranslationX.toInt()
        bottom = top + mTheLastRowMargin
        mRect.set(left, top, right, bottom)
        mPaint.color = mTheLastRowMarginColor
        if (mRect.height() > 0) {
            c.drawRect(mRect, mPaint)
        }
    }

    private fun drawFirstRow(
        c: Canvas,
        parent: RecyclerView,
        childCount: Int,
        span: Int,
        child: View,
        childPos: Int,
        childParams: RecyclerView.LayoutParams
    ) {
        val parentParams = parent.layoutParams
        val parentPaddingLeft = parent.paddingLeft
        val parentPaddingTop = parent.paddingTop
        val parentPaddingRight = parent.paddingRight
        val parentPaddingBottom = parent.paddingBottom


        val childTranslationX = child.translationX
        val childTranslationY = child.translationY

        var left = 0
        var top = 0
        var right = 0
        var bottom = 0
        //画view顶部的背景
        if (isFirstColumn(childPos, span)) {
            left = parentPaddingLeft + childTranslationX.toInt()
        } else {
            left = child.left - childParams.leftMargin - mHorizontalMargin + childTranslationX.toInt()
        }
        top = parentPaddingTop + childTranslationY.toInt()
//        right = child.right + childParams.rightMargin + (if (isTheLastColumn(childPos, span)) mTheLastColumnMargin else mHorizontalMargin) + childTranslationX.toInt()
        right = child.right + childParams.rightMargin + (if (isTheLastColumn(childPos, span)) mTheLastColumnMargin else 0)  + childTranslationX.toInt()
        bottom = child.top - childParams.topMargin + childTranslationY.toInt()
        mRect.set(left, top, right, bottom)
        mPaint.color = mFirstRowMarginColor
        if (mRect.height() > 0) {
            c.drawRect(mRect, mPaint)
        }
        //画view左边的背景
        right = child.left - childParams.leftMargin + childTranslationX.toInt()
        top = child.top + childTranslationY.toInt()
        bottom = child.bottom + childTranslationY.toInt()
        mRect.set(left, top, right, bottom)
        mPaint.color = if (isFirstColumn(childPos, span)) mFirstColumnMarginColor else mHorizontalMarginColor
        if (mRect.width() > 0) {
            c.drawRect(mRect, mPaint)
        }

        //如果是第一行并且是最后一列，画view右边背景
        if (isTheLastColumn(childPos, span)) {
            left = child.right + childParams.rightMargin + childTranslationX.toInt()
            top = child.top + childTranslationY.toInt()
//            right = parent.measuredWidth - parentPaddingRight + childTranslationX.toInt()
            right = child.right + childParams.rightMargin + mTheLastColumnMargin + childTranslationX.toInt()
            bottom = child.bottom + childTranslationY.toInt()
            mRect.set(left, top, right, bottom)
            mPaint.color = mTheLastColumnMarginColor
            if (mRect.width() > 0) {
                c.drawRect(mRect, mPaint)
            }
        }

        //如果是第一行 并且是最后一行,画view底部背景
        if (isTheLastRow(childPos, span, childCount)) {
            if (isFirstColumn(childPos, span)) {
                left = parentPaddingLeft + childTranslationX.toInt()
            } else {
                left = child.left - childParams.leftMargin - mHorizontalMargin + childTranslationX.toInt()
            }
            top = child.bottom + childParams.bottomMargin + childTranslationY.toInt()
//            right = child.right + childParams.rightMargin + (if (isTheLastColumn(childPos, span)) mTheLastColumnMargin else mHorizontalMargin) + childTranslationX.toInt()
            right = child.right + childParams.rightMargin + (if (isTheLastColumn(childPos, span)) mTheLastColumnMargin else 0) + childTranslationX.toInt()
            bottom = top + mTheLastRowMargin
            mRect.set(left, top, right, bottom)
            mPaint.color = mTheLastRowMarginColor
            if (mRect.height() > 0) {
                c.drawRect(mRect, mPaint)
            }
        }
    }
}